package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Option;
import com.sl.au.entity.Part;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.repository.OptionRepository;
import com.sl.au.repository.PartBaseInfoRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.IdentifyService;
import com.sl.au.service.PartBaseInfoService;

@Service("PartBaseInfoService")
@Transactional
public class PartBaseInfoServiceImpl extends BaseServiceImpl implements PartBaseInfoService {
	@Autowired
	PartBaseInfoRepository partBaseInfoRepository;

	@Autowired
	OptionRepository optionRepository;

	@Autowired
	IdentifyService identifyService;

	public PartBaseInfoRepository getRepository() {
		return this.partBaseInfoRepository;
	}

	@Override
	public List<PartBaseInfo> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByKey(key, pageable);
		} else {
			pages = this.partBaseInfoRepository.findAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return this.partBaseInfoRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		return this.partBaseInfoRepository.getByKey(key, pageable).getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByNoPart(int start, int length, String key, String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByNoPartAndKey(key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByNoPart(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoPart(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByNoPart(pageable);
		} else {
			pages = this.partBaseInfoRepository.getByNoPartAndKey(key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByPart(int start, int length, String partId, String key, String orderName,
			Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByPartAndKey(partId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByPart(partId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByPart(String partId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByPart(partId, pageable);
		} else {
			pages = this.partBaseInfoRepository.getByPartAndKey(partId, key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByPartAndGrid(int start, int length, String partId, String gridId, String key,
			String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByPartAndGridAndKey(partId, gridId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByPartAndGrid(partId, gridId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByPartAndGrid(String partId, String gridId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByPartAndGridAndKey(partId, gridId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByPartAndGrid(partId, gridId, pageable);
		}
		return pages.getContent().size();
	}

	/****************** 部件权属部门相关查询 *******************/

	@Override
	public List<PartBaseInfo> getPageByNoHierarchy(int start, int length, String key, String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByNoHierarchyAndKey(key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByNoHierarchy(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoHierarchy(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByNoHierarchy(pageable);
		} else {
			pages = this.partBaseInfoRepository.getByNoHierarchyAndKey(key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByHierarchy(int start, int length, String hierarchyId, String key,
			String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByHierarchyAndKey(hierarchyId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByHierarchy(hierarchyId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByHierarchy(String hierarchyId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByHierarchy(hierarchyId, pageable);
		} else {
			pages = this.partBaseInfoRepository.getByHierarchyAndKey(hierarchyId, key, pageable);
		}
		return pages.getContent().size();
	}

	/****************** 部件管理部门相关查询 *******************/

	@Override
	public List<PartBaseInfo> getPageByNoManageHierarchy(int start, int length, String key, String orderName,
			Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByNoManageHierarchyAndKey(key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByNoManageHierarchy(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoManageHierarchy(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByNoManageHierarchy(pageable);
		} else {
			pages = this.partBaseInfoRepository.getByNoManageHierarchyAndKey(key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByManageHierarchy(int start, int length, String hierarchyId, String key,
			String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByManageHierarchyAndKey(hierarchyId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByManageHierarchy(hierarchyId, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByManageHierarchy(String hierarchyId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByHierarchy(hierarchyId, pageable);
		} else {
			pages = this.partBaseInfoRepository.getByHierarchyAndKey(hierarchyId, key, pageable);
		}
		return pages.getContent().size();
	}

	/****************** 部件维护部门相关查询 *******************/

	@Override
	public List<PartBaseInfo> getPageByNoMaintainHierarchy(int start, int length, String key, String orderName,
			Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByNoMaintainHierarchyAndKey(key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByNoMaintainHierarchy(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByNoMaintainHierarchy(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByNoMaintainHierarchy(pageable);
		} else {
			pages = this.partBaseInfoRepository.getByNoMaintainHierarchyAndKey(key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<PartBaseInfo> getPageByMaintainHierarchy(int start, int length, String hierarchyId, String key,
			String orderName, Direction dir) {
		Page<PartBaseInfo> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getByMaintainHierarchyAndKey(hierarchyId, key, pageable);
		} else {
			pages = this.partBaseInfoRepository.getAllByMaintainHierarchy(hierarchyId, pageable);
		}

		return pages.getContent();
	}

	/********************************************************/

	@Override
	public int getCountByMaintainHierarchy(String hierarchyId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (!StringUtils.hasText(key)) {
			pages = this.partBaseInfoRepository.getAllByHierarchy(hierarchyId, pageable);
		} else {
			pages = this.partBaseInfoRepository.getByHierarchyAndKey(hierarchyId, key, pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public PartBaseInfo getPartBaseInfoByName(String name) {
		PartBaseInfo partBaseInfo = new PartBaseInfo();
		partBaseInfo = this.partBaseInfoRepository.findByName(name);
		return partBaseInfo;
	}

	@Override
	public PartBaseInfo savePartBaseInfo(PartBaseInfo partBaseInfo) {
		String id = partBaseInfo.getId();
		String stateId = partBaseInfo.getState().getId();
		PartBaseInfo baseInfo = null;
		if ("".equals(id)) {
			baseInfo = new PartBaseInfo();
			Option state = optionRepository.findOne(stateId);
			baseInfo.setState(state);
			if (partBaseInfo.getGridAreaInfo() != null) {
				baseInfo.setGridAreaInfo(partBaseInfo.getGridAreaInfo());

				if (partBaseInfo.getGridAreaInfo().getDivision() != null) {
					String divisionIdentify = identifyService.getIdentifyByType(
							partBaseInfo.getGridAreaInfo().getDivision().getId(), "gridShow", "division");
					baseInfo.setDivisionIdentify(divisionIdentify);
				} else {
					baseInfo.setDivisionIdentify(null);
				}
			}
			if (partBaseInfo.getPart() != null) {
				int orderNum = identifyService.getPartBaseInfoOrderByPart(partBaseInfo.getPart().getId());
				if (orderNum < 1000) {
					String partIdentify = identifyService.getIdentifyByType(partBaseInfo.getPart().getId(), "partShow",
							"part");
					String formatOrderNum = String.format("%03d", orderNum);
					partIdentify += formatOrderNum;
					baseInfo.setPart(partBaseInfo.getPart());
					baseInfo.setPartOrderNum(orderNum);
					baseInfo.setPartIdentify(partIdentify);
				} else {
					return null;
				}
			}
		} else {
			baseInfo = partBaseInfoRepository.findOne(id);
			if (!stateId.equals(baseInfo.getState().getId())) {
				Option state = optionRepository.findOne(stateId);
				baseInfo.setState(state);
			}
			if (baseInfo.getGridAreaInfo() == null
					|| partBaseInfo.getGridAreaInfo()==null||!baseInfo.getGridAreaInfo().getId().equals(partBaseInfo.getGridAreaInfo().getId())) {
				if (partBaseInfo.getGridAreaInfo() != null) {
					baseInfo.setGridAreaInfo(partBaseInfo.getGridAreaInfo());
					if (partBaseInfo.getGridAreaInfo().getDivision() != null) {
						String divisionIdentify = identifyService.getIdentifyByType(
								partBaseInfo.getGridAreaInfo().getDivision().getId(), "gridShow", "division");
						baseInfo.setDivisionIdentify(divisionIdentify);
					} else {
						baseInfo.setDivisionIdentify(null);
					}
				}

			}

		}

		if (partBaseInfo.getIconAddress() != null) {
			baseInfo.setIconAddress(partBaseInfo.getIconAddress());
		}

		baseInfo.setPartAdress(partBaseInfo.getPartAdress());
		baseInfo.setPartName(partBaseInfo.getPartName());
		baseInfo.setManufacturer(partBaseInfo.getManufacturer());
		baseInfo.setInstallationTime(partBaseInfo.getInstallationTime());
		baseInfo.setProductionTime(partBaseInfo.getProductionTime());
		baseInfo.setLatitude(partBaseInfo.getLatitude());
		baseInfo.setLongitude(partBaseInfo.getLongitude());

		baseInfo.setDeviceIdentify(partBaseInfo.getDeviceIdentify());
		baseInfo.setInterfaceAddress(partBaseInfo.getInterfaceAddress());
		baseInfo.setInterfacePort(partBaseInfo.getInterfacePort());
		// if(partBaseInfo.getPart()!=null){
		// baseInfo.setPart(partBaseInfo.getPart());
		// }
		partBaseInfoRepository.save(baseInfo);

		return baseInfo;
	}

	@Override
	public List<PartBaseInfo> getPageByGridAreaInfoAndKey(int start, int length, GridAreaInfo gridAreaInfo, String key,
			String orderName, Direction dir) {
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		Page<PartBaseInfo> pages = null;
		if (StringUtils.hasText(key)) {
			pages = partBaseInfoRepository.findByGridAreaInfoAndKey(gridAreaInfo, key, pageable);
		} else {
			pages = partBaseInfoRepository.findByGridAreaInfo(gridAreaInfo, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByGridAreaInfoAndKey(GridAreaInfo gridAreaInfo, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<PartBaseInfo> pages = null;
		if (StringUtils.hasText(key)) {
			pages = partBaseInfoRepository.findByGridAreaInfoAndKey(gridAreaInfo, key, pageable);
		} else {
			pages = partBaseInfoRepository.findByGridAreaInfo(gridAreaInfo, pageable);
		}
		return pages.getContent().size();
	}

	/**
	 * 点击区域 网格 部件 树根节点,显示所有已绑定网格的视频部件
	 */
	@Override
	public List<PartBaseInfo> findBindPartOnlyCon(String con, String typeId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartOnlyCon(pageable, con, typeId).getContent();
	}

	@Override
	public int findBindPartCountOnlyCon(String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartOnlyCon(pageable, con, typeId).getContent().size();
	}

	/**
	 * 点击区域 网格 部件 树的根节点,显示所有已绑定网格的视频部件
	 */
	@Override
	public List<PartBaseInfo> findBindPartNone(String typeId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartNone(pageable, typeId).getContent();
	}

	@Override
	public int findBindPartCountNone(String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartNone(pageable, typeId).getContent().size();
	}

	/**
	 * 点击区域 网格 部件 树的 区域节点,显示所有已绑定网格的视频部件
	 */
	@Override
	public List<PartBaseInfo> findBindPartByGridIdsAndCon(int start, int length, String[] gridIds, String con,
			String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByGridIdsAndCon(pageable, gridIds, con, typeId).getContent();
	}

	@Override
	public int findBindPartCountByGridIdsAndCon(String[] gridIds, String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByGridIdsAndCon(pageable, gridIds, con, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findBindPartByGridIds(String[] gridIds, int start, int length, String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByGridIds(pageable, gridIds, typeId).getContent();
	}

	@Override
	public int findBindPartCountByGridIds(String[] gridIds, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByGridIds(pageable, gridIds, typeId).getContent().size();
	}

	/**
	 * 点击部件本身
	 */
	@Override
	public List<PartBaseInfo> findBindPartByPartIdsAndCon(int start, int length, String[] partIds, String con,
			String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByPartIdsAndCon(pageable, partIds, con, typeId).getContent();
	}

	@Override
	public int findBindPartCountByPartIdsAndCon(String[] partIds, String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByPartIdsAndCon(pageable, partIds, con, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findBindPartByPartIds(String[] partIds, int start, int length, String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByPartIds(pageable, partIds, typeId).getContent();
	}

	@Override
	public int findBindPartCountByPartIds(String[] partIds, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findBindPartByPartIds(pageable, partIds, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findUnbindPartOnlyCon(String con, String typeId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartOnlyCon(pageable, con, typeId).getContent();
	}

	@Override
	public int findUnbindPartCountOnlyCon(String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartOnlyCon(pageable, con, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findUnbindPartNone(String typeId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartNone(pageable, typeId).getContent();
	}

	@Override
	public int findUnbindPartCountNone(String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartNone(pageable, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findUnbindPartByGridIdsAndCon(int start, int length, String[] gridIds, String con,
			String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartByGridIdsAndCon(pageable, gridIds, con, typeId).getContent();
	}

	@Override
	public int findUnbindPartCountByGridIdsAndCon(String[] gridIds, String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartByGridIdsAndCon(pageable, gridIds, con, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findUnbindPartByGridIds(String[] gridIds, int start, int length, String typeId) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartByGridIds(pageable, gridIds, typeId).getContent();
	}

	@Override
	public int findUnbindPartCountByGridIds(String[] gridIds, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return partBaseInfoRepository.findUnbindPartByGridIds(pageable, gridIds, typeId).getContent().size();
	}

	@Override
	public List<PartBaseInfo> findUnbindPartByPartIdsAndCon(int start, int length, String[] partIds, String con,
			String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return null;
	}

	@Override
	public int findUnbindPartCountByPartIdsAndCon(String[] partIds, String con, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return 0;
	}

	@Override
	public List<PartBaseInfo> findUnbindPartByPartIds(String[] partIds, int start, int length, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return null;
	}

	@Override
	public int findUnbindPartCountByPartIds(String[] partIds, String typeId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return 0;
	}

}
